//验证二叉搜索树
//测试链接：https://leetcode.cn/problems/validate-binary-search-tree/
public class IsValidBST {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    /**
     *中序遍历看是否有序
     */
    long preNum = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;

        boolean left = isValidBST(root.left);
        if(!left) return false;  //剪枝

        boolean cur = false;
        if( root.val > preNum) cur = true;

        preNum = root.val;

        boolean right = isValidBST(root.right);
        if(!right ) return false; //剪枝


        return left && cur && right;
    }
}
